package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.IncludeOtherException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mappper.SetmealMapper;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service

public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private DishService dishService;

    @Transactional
    @Override
    public boolean saveWithDish(SetmealDto setmealDto) {
        //保存菜品信息
        this.save(setmealDto);

        //获取菜品信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            //将列表下的每一个菜品 设置其套餐id
            setmealDish.setSetmealId(setmealDto.getId());
        }
        //保存 套餐_菜品
        setmealDishService.saveBatch(setmealDishes);

        return true;
    }

    @Override
    public SetmealDto selectWithDish(Long id) {
        //根据id先获取套餐信息
        Setmeal setmeal = this.getById(id);

        //再获取套餐下的菜品信息
        LambdaQueryWrapper<SetmealDish> setmealWrapper = new LambdaQueryWrapper<>();
        setmealWrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> setmealDishes = setmealDishService.list(setmealWrapper);

        //组装dto
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        setmealDto.setSetmealDishes(setmealDishes);

        return setmealDto;
    }

    @Transactional
    @Override
    public boolean updateWithDish(SetmealDto setmealDto) {
        //先修改套餐信息
        this.updateById(setmealDto);

        //直接删除有关该套餐下的所有菜品
        LambdaQueryWrapper<SetmealDish> setmealDishyWrapper = new LambdaQueryWrapper<>();
        setmealDishyWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(setmealDishyWrapper);

        //再根据传过来的数据重新添加菜品信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
        }
        setmealDishService.saveBatch(setmealDishes);

        return true;
    }

    @Transactional
    @Override
    public boolean deleteByIds(List<Long> ids) {
        //查询出所有的套餐是否处于出售状态
        LambdaQueryWrapper<Setmeal> setmealWrapper = new LambdaQueryWrapper<>();
        setmealWrapper.in(Setmeal::getId,ids);
        setmealWrapper.eq(Setmeal::getStatus,1);
        int statusCount = this.count(setmealWrapper);

        //大于0说明有处于出售状态
        if (statusCount > 0){
            throw new IncludeOtherException("删除失败，该套餐处于出售状态，需要先停售！");
        }
        //删除套餐
        this.removeByIds(ids);

        //删除套餐下的菜品信息
        LambdaQueryWrapper<SetmealDish> setmealDishWrapper = new LambdaQueryWrapper<>();
        setmealDishWrapper.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(setmealDishWrapper);

        return true;
    }

    @Override
    public void updateStatusByIds(Integer status, List<Long> ids) {

        //需要起售时
        if (status == 1){
            //查询出该套餐下所关联下的所有菜品
            LambdaQueryWrapper<SetmealDish> setmealWrapper = new LambdaQueryWrapper<>();
            setmealWrapper.in(SetmealDish::getSetmealId,ids);
            List<SetmealDish> setmealDishes = setmealDishService.list(setmealWrapper);

            //拿出该菜品id
            List<Long> dishIds = setmealDishes.stream().map(
                    setmealDish -> {
                        return setmealDish.getDishId();
                    }
            ).collect(Collectors.toList());

            //直接去查询数据库看菜品列表是否含有未起售的物品
            LambdaQueryWrapper<Dish> dishWrapper = new LambdaQueryWrapper<>();
            dishWrapper.in(Dish::getId,dishIds);
            dishWrapper.eq(Dish::getStatus,0);

            int dishStatusCount = dishService.count(dishWrapper);

            if (dishStatusCount > 0){
                throw new IncludeOtherException("该套餐下有未起售的菜品，请先起售");
            }
        }
        //查询出所有的套餐
        List<Setmeal> setmeals = this.listByIds(ids);
        //修改套餐状态
        setmeals.stream().forEach(
                setmeal -> {
                    setmeal.setStatus(status);
                }
        );

        this.updateBatchById(setmeals);

    }
}
